"""__author__=Hanlin"""
import json
from pprint import pprint
import re

import requests

def get_page(page):
    url = 'https://movie.douban.com/top250?start=%s' % str(page * 25)
    headers = {"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Mobile Safari/537.36"
    }

    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        return response.content.decode('utf-8')
    return None


def parse_page(html):
    pattern = re.compile('<a href=.*?<span class="title">(.*?)</span>', re.S)
    titles = re.findall(pattern, html)
    # print(titles)

    pattern = re.compile('<span class="rating_num" property="v:average">(.*?)</span>', re.S)
    scores = re.findall(pattern, html)


    pattern = re.compile('</em>.*?<a href.*?<img.*?src="(.*?)" class="">', re.S)
    covers = re.findall(pattern, html)

    result = []
    for i in range(len(titles)):
        data_dict = {
            'title': titles[i],
            'score': scores[i],
            'cover': covers[i]
        }

        result.append(data_dict)
    return result


def save_json(result_list):
    json_str = json.dumps(result_list, ensure_ascii=False)
    with open('douban.json', 'w', encoding='utf-8')as f:
        f.write(json_str)

def get_pages():
    result_list = []
    for page in range(10):
        html = get_page(page)
        result = parse_page(html)
        result_list.extend(result)
    return result_list


def main():
    result_list = get_pages()
    pprint(result_list)
    save_json(result_list)


if __name__ == '__main__':
    main()